package com.example.hellodemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PopupWindowActivity extends AppCompatActivity {
    private Button popBtn;
    private PopupWindow pop;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popupwindow);
        popBtn = findViewById(R.id.pop_btn);
        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                View view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.pop_window,null);
                TextView textView1 = view.findViewById(R.id.popBtn1);
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(PopupWindowActivity.this,((TextView)v).getText(),Toast.LENGTH_LONG).show();
                        pop.dismiss();

                    }
                });
                pop = new PopupWindow(view, popBtn.getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT);
                pop.setOutsideTouchable(true); // 取消遮罩点击效果
                pop.setFocusable(true); // 再次点击聚焦 便取消
                pop.showAsDropDown(popBtn);
            }
        });

    }
}
